let http = require("http");
let url = require("url");

// 创建服务器
let server = http.createServer();
// 绑定请求事件
server.on("request", (req, res) => {
  let urlObj = new URL(req.url, "http://localhost:3000/");
  console.log(urlObj);
  //   定义特定接口
  switch (urlObj.pathname) {
    case "/favicon.ico":
      res.writeHead(200);
      res.end();
      break;

    // 提供jsonp接口
    case "/myapi/a":
      res.writeHead(200, { "Content-Type": "text/plain;charset=utf-8" });
      res.end(
        `${urlObj.searchParams.get("callback")}(${JSON.stringify({
          name: "zs",
          age: "18",
        })})`
      );
      break;

    default:
      res.writeHead(404);
      res.end("404 not found");
      break;
  }
});

server.listen(3000, () => {
  console.log("3000 port");
});
